const express = require('express');
const router = express.Router();
const authMiddleware = require('../middlewares/auth.middleware');

// 导入控制器
const authController = require('../controllers/auth.controller');
const userController = require('../controllers/user.controller');
const postController = require('../controllers/post.controller');
const commentController = require('../controllers/comment.controller');
const tagController = require('../controllers/tag.controller');

// 认证路由
router.post('/auth/login', authController.login);
router.post('/auth/register', authController.register);
router.post('/auth/refresh-token', authController.refreshToken);
router.post('/auth/logout', authMiddleware.verifyToken, authController.logout);

// 用户路由
router.get('/users', authMiddleware.verifyToken, authMiddleware.isAdmin, userController.getAllUsers);
router.get('/users/:id', authMiddleware.verifyToken, userController.getUserById);
router.post('/users', authMiddleware.verifyToken, authMiddleware.isAdmin, userController.createUser);
router.put('/users/:id', authMiddleware.verifyToken, userController.updateUser);
router.delete('/users/:id', authMiddleware.verifyToken, userController.deleteUser);
router.get('/me', authMiddleware.verifyToken, userController.getCurrentUser);

// 文章路由 (注意: 特定路由应在通用路由之前)
router.get('/posts/page', postController.getPublicPosts);
router.get('/posts/:id', postController.getPostById);
router.get('/posts', postController.getAllPosts);
router.post('/posts', authMiddleware.verifyToken, postController.createPost);
router.put('/posts/:id', authMiddleware.verifyToken, postController.updatePost);
router.delete('/posts/:id', authMiddleware.verifyToken, postController.deletePost);

// 评论路由
router.get('/posts/:postId/comments', commentController.getCommentsByPost);
router.get('/comments/:id', commentController.getCommentById);
router.post('/comments', authMiddleware.verifyToken, commentController.createComment);
router.patch('/comments/:id/status', authMiddleware.verifyToken, authMiddleware.isAdmin, commentController.updateCommentStatus);
router.delete('/comments/:id', authMiddleware.verifyToken, commentController.deleteComment);

// 标签路由 (注意: 特定路由应在通用路由之前)
router.get('/tags/:id', tagController.getTagById);
router.get('/tags', tagController.getAllTags);
router.get('/posts/:postId/tags', tagController.getTagsByPost);
router.post('/tags', authMiddleware.verifyToken, authMiddleware.isAdmin, tagController.createTag);
router.put('/tags/:id', authMiddleware.verifyToken, authMiddleware.isAdmin, tagController.updateTag);
router.delete('/tags/:id', authMiddleware.verifyToken, authMiddleware.isAdmin, tagController.deleteTag);

module.exports = router;